<?php 

require 'config/dbconfig.php';

//Get posted values
$contestant = $_POST['vote'];
$challenge_id = $_POST['challenge_id'];

$user = $_SESSION['id'];

//Did the user already vote for this contest?
$query = "SELECT * FROM votes WHERE vote_user_id='$user' AND vote_challenge_id='$challenge_id' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
    //USER ALREADY VOTED
	exit("You already voted for this contest!");
}

$query = mysql_query("INSERT INTO votes (vote_user_id, vote_contestant_id, vote_challenge_id) VALUES ('$user', '$contestant', '$challenge_id')") or die(mysql_error());

//Update the current vote ticker in the contestant table
$sqlquery_user=mysql_query("SELECT votes FROM contestants WHERE id = '$contestant'"); 
while ($row= mysql_fetch_assoc($sqlquery_user)) {
$vote_count = $row['votes'];
$new_vote = $vote_count + 1;
$sqlquery_user = mysql_query("UPDATE contestants SET votes = '$new_vote' WHERE id = '$contestant'") or die(mysql_error());
}


?>